/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */
package com.yunbiji.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yunbiji.util.GetTime;
import com.yunbiji.util.Md5Util;

@SuppressWarnings("serial")
/**
 * 用户模型类
 * @author 聂道鑫
 *
 */
public class Users extends Model<Users>
{
	private String LOGIN_SQL = "SELECT * FROM users WHERE tel=? and a_userpass=?";
	
	private String REGISTER_SQL = "insert into users (a_username,a_email,a_qq,tel,a_userpass,a_allowlogin,a_usertype) values(?,?,?,?,?,?,?)";
	
	private String IfReg_SQL="SELECT * FROM users WHERE tel=?";
	
	private String updateUserProfile_SQL="update users set a_username=?,a_job=?,a_birth=?,a_email=?,a_qq=?where tel=?";
	
	private String updateUserProfileMem_SQL="update users set a_username=?,a_userpass=?,tel=?,a_birth=?,a_email=?,a_qq=?,a_allowlogin=?,a_text=? where id=?";
	
	private String updateUserProfileMemNoChangePwd_SQL="update users set a_username=?,tel=?,a_birth=?,a_email=?,a_qq=?,a_allowlogin=?,a_text=? where id=?";
	
	private String oldPasswd_SQL="select a_userpass from users where tel=?";
	
	private String updatePasswd_SQL="update users set a_userpass=? where tel=?";
	
	private String getMembersSql = "from users where a_faaccount =? and a_folder =?";
	
	private String allowloginSql ="select a_allowlogin from users where tel=?";
	
	private String getMembersByIdSql="SELECT * FROM users WHERE id=?";
	
	private String getMembersPwdByIdSql="SELECT a_userpass FROM users WHERE id=?";
	
	private String getMembersTelByIdSql="SELECT tel FROM users WHERE id=?";
	
	
	public static final Users dao = new Users();
	
	/**
	 * 判断账号是否允许登录
	 * @param username
	 * @return
	 */
	public int allowLogin(String username)
	{
		return(Db.queryInt(allowloginSql, username));
	}

	/**
	 * 
	 * @param username
	 * @param passwd
	 * @return flag
	 * 密码采用MD5加密
	 */
	public boolean login(String username, String passwd)
	{
		if (null != Db.findFirst(LOGIN_SQL, username,Md5Util.getMD5(passwd)))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * 注册用户信息
	 * @param name
	 * @param email
	 * @param qq
	 * @param tel
	 * @param passwd
	 * @return
	 */
	public boolean reg(String name,String email,String qq,String tel,String passwd)
	{
		int i= Db.update(REGISTER_SQL,name,email,qq,tel,Md5Util.getMD5(passwd),1,"管理员");


		if(i==1)
		{
			return true;	
		}
		else
		{
			return false;
		}
	}
	/**
	 * 判断手机号是否已经注册过了
	 * @author niedaoxin
	 * @param tel
	 * @return
	 */
	public boolean Ifreg(String tel)
	{
		if(null!=Db.findFirst(IfReg_SQL,tel))
		{
			return false;	
		}
		else
		{
			return true;
		}
	}
	/**
	 * 获取当前用户的所有信息
	 * @param tel
	 * @return
	 */
	public List<Users> getUserProfile(String tel)
	{
		List<Users> list=Users.dao.find(IfReg_SQL, tel);
		return list;
	}
	
	/**
	 * 更新个人中心中用户修改的信息
	 * @param a_username
	 * @param a_job
	 * @param a_birth
	 * @param a_email
	 * @param a_qq
	 * @param a_paging
	 * @param tel
	 * @return
	 */
	public boolean updateUserProfile(String a_username,String a_job,String a_birth,String a_email,String a_qq,String tel)
	{
		int flag=Db.update(updateUserProfile_SQL, a_username,a_job,a_birth,a_email,a_qq,tel);
		if(flag==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * 更新个人密码
	 * @param oldPasswd
	 * @param changePasswd
	 * @param tel
	 * @return
	 */
	public boolean changePassword(String oldPasswd,String changePasswd,String tel)
	{
		String passwd =Db.queryStr(oldPasswd_SQL, tel);
		if(passwd.equals(Md5Util.getMD5(oldPasswd)))
		{
			int i=Db.update(updatePasswd_SQL, Md5Util.getMD5(changePasswd),tel);
			
			if(i==1)
				return true;
			else
				return false;
		}
		else 
			return false;
	}

	/**
	 * 获取当前组内的组员列表
	 * @param tel
	 * @param pIdMem
	 * @param pagenoMem
	 * @return
	 */
	public List<Users> getUsers(String tel, String pIdMem, Integer pagenoMem)
	{
		/**
		 * 获取分页的列表内容
		 * @author niedaoxin
		 * @param tel
		 * @param idString
		 * @param pageno
		 * @return list
		 */
		int id=Integer.valueOf(pIdMem);
		Page<Users> page = Users.dao.paginate(pagenoMem, 5, " select * ",getMembersSql,tel,id);
		List<Users> list = page.getList();
		return list;
	}
	/**
	 * 新增成员
	 * @param tel
	 * @param noteName
	 * @param pId
	 */
	
	public void updateUsers(String tel,String menName,String pId )
	{
		//设置tel为
		Db.update("insert into users (a_username,a_faaccount,a_foundtime,a_folder) values(?,?,?,?)",menName,tel,GetTime.dao.getTime(),Integer.valueOf(pId));
	}
	
	/**
	 * 将刚刚新增的成员的id取出
	 * @param tel
	 * @return
	 */
	public int queryIntId(String tel)
	{
		return (Db.queryInt("select max(id) from users where a_faaccount=?",tel));
	}
	/**
	 * 根据id取出对应成员的所有信息
	 * @param id
	 * @return
	 */
	public  Record getMembersById(int id)
	{
		Record model= Db.findFirst(getMembersByIdSql, id);
		return model;
		
	}

	/**
	 * 更新成员的个人资料(改动过密码的)
	 * @param usernameMem
	 * @param passwordMem
	 * @param telMem
	 * @param birthMem
	 * @param emailMem
	 * @param qqMem
	 * @param allowMem
	 */
	public boolean updateUserProfileMem(String usernameMem, String passwordMem,String telMem, String birthMem, String emailMem, String qqMem,String allowMem,String contentMem,int idMem)
	{
		int i= Db.update(updateUserProfileMem_SQL,usernameMem,Md5Util.getMD5(passwordMem),telMem,birthMem,emailMem,qqMem,Integer.valueOf(allowMem),contentMem,idMem);


		if(i==1)
		{
			return true;	
		}
		else
		{
			return false;
		}
		
	}
/**
 * 更新成员的个人资料(未改动过密码的)
 * @param usernameMem
 * @param telMem
 * @param birthMem
 * @param emailMem
 * @param qqMem
 * @param allowMem
 * @param contentMem
 * @param idMem
 * @return
 */
	public boolean updateUserProfileMemNoChangePwd(String usernameMem,String telMem, String birthMem, String emailMem, String qqMem,String allowMem,String contentMem,int idMem)
	{
		int i= Db.update(updateUserProfileMemNoChangePwd_SQL,usernameMem,telMem,birthMem,emailMem,qqMem,Integer.valueOf(allowMem),contentMem,idMem);


		if(i==1)
		{
			return true;	
		}
		else
		{
			return false;
		}
		
	}
	/**
	 * 根据id获取当前成员的密码
	 * @param idMem
	 * @return
	 */
	public String getOldString(int idMem)
	{
		
		String s=Db.queryStr(getMembersPwdByIdSql, idMem);
		return s;
	}
	/**
	 * 根据id获取当前成员的手机号码
	 * @param object
	 * @return
	 */
	public String getMembersTelById(int id)
	{
		String s=Db.queryStr(getMembersTelByIdSql, id);
		return s;
	}

}
